/*
 * ----------------------------------------------------------------------- Copyright 2012 - Alistair
 * Rutherford - www.netthreads.co.uk
 * -----------------------------------------------------------------------
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.garrigou.spaceship.action;

import aurelienribon.tweenengine.Timeline;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.garrigou.spaceship.SpaceShip;
import com.garrigou.spaceship.utils.ActionResetingPool;

/**
 * Time-line action runs a Tween time-line separate from the TweenManager.
 * 
 */
public class TimelineAction extends Action
{

    private static final ActionResetingPool<TimelineAction> pool = new ActionResetingPool<TimelineAction>(
                                                                         10,
                                                                         100)
                                                                 {
                                                                     @Override
                                                                     protected TimelineAction newObject()
                                                                     {
                                                                         TimelineAction action = new TimelineAction();

                                                                         return action;
                                                                     }
                                                                 };

    private Timeline                                        timeline;
    private Actor                                           target;
    protected boolean                                       done;

    /**
     * Get instance from pool.
     * 
     * @param timeline
     *        The action time-line to execute.
     * 
     * @return Pooled instance.
     */
    public static TimelineAction $(Timeline timeline)
    {
        TimelineAction action = pool.obtain();

        action.setTimeline(timeline);

        return action;
    }

    @Override
    public boolean act(float delta)
    {
        if (!isDone())
        {
            timeline.update((int) (delta * 1000));
        }
        else
        {
            if (SpaceShip.DEV_MODE)
                Gdx.app.log(TAG, "!!!Executing finished timeline, "
                        + timeline.getClass().hashCode());
        }
        return done;
    }

    // @Override
    public boolean isDone()
    {
        return timeline.isFinished();
    }

    // @Override
    public Action copy()
    {
        return this;
    }

    // @Override
    public void setTarget(Actor actor)
    {
        this.target = actor;
    }

    // @Override
    public Actor getTarget()
    {
        return target;
    }

    @Override
    public void reset()
    {
        super.reset();

// pool.free(this);
// timeline.free();
    }

    // @Override
    public void finish()
    {
        // super.finish();
        pool.free(this);
        timeline.free();
    }

    private void setTimeline(Timeline timeline)
    {
        this.timeline = timeline;
    }

    /**
     * Fetch TimeLine reference.
     * 
     * @return The TimeLine reference.
     */
    public Timeline getTimeline()
    {
        return timeline;
    }

    /**
     * Set done status.
     * 
     * @param done
     */
    public void setDone(boolean done)
    {
        this.done = done;
    }
    
    private final static String TAG = TimelineAction.class.getName();
}
